import 'dart:io';

import 'package:dio/dio.dart';
import 'package:package_info_plus/package_info_plus.dart';

import '../../../utils/bt_tool_utils.dart';
import '../../store/bt_hive_logon_store.dart';
import 'package:device_info_plus/device_info_plus.dart';

class BtReqHeadersInterceptor extends Interceptor {
  @override
  Future<void> onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
    String deviceId = await getDeviceId();
    Map<String, dynamic> headers = options.headers;
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    var packageInfo = await PackageInfo.fromPlatform();
    String version = packageInfo.version;
    String bundleId = packageInfo.packageName;
    String buildNumber = packageInfo.buildNumber;
    var data = await BtHiveLogonStore().getData();
    String accessToken = data?.token ?? '';
    if (headers["authorized"] != 'false' && accessToken.isNotEmpty) {
      headers["Authorization"] = 'Bearer$accessToken';
    }
    headers['device-id'] = deviceId;
    if (Platform.isAndroid) {
      AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      headers['model'] = androidInfo.model;
      headers['platform'] = 'Android';
    } else if (Platform.isIOS) {
      IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
      headers['model'] = iosInfo.model;
      headers["platform"] = "iOS";
    }
    headers['pkg'] = bundleId; // 包名
    headers['ver'] = version; // 版本 例如1.0.0，读取实际配置的版本号
    headers['p_ver'] = buildNumber;

    super.onRequest(options, handler);
  }
}
